package com.ljwm.seckill.web;

import com.ljwm.seckill.dto.Exposer;
import com.ljwm.seckill.dto.SeckillExecution;
import com.ljwm.seckill.dto.SeckillResult;
import com.ljwm.seckill.entity.Seckill;
import com.ljwm.seckill.enums.SeckillState;
import com.ljwm.seckill.exception.RepeatKillException;
import com.ljwm.seckill.exception.SeckillCloseException;
import com.ljwm.seckill.service.SeckillService;
import com.ljwm.seckill.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * Created by yuzhou on 2017/8/7.
 */
@Controller
@RequestMapping("/seckill")
public class SeckillController {

  private static final Logger _log = LoggerFactory.getLogger(SeckillController.class);

  @Autowired
  private SeckillService seckillService;

  @GetMapping("/list")
  public String list(Model model) {
    List<Seckill> list = seckillService.getSeckillList();
    model.addAttribute("list", list);
    return "list";
  }

  @GetMapping("/{seckillId}/detail")
  public String detail(@PathVariable Long seckillId, Model model) {
    if (seckillId == null) {
      return "redirect: /seckill/list";
    }
    Seckill seckill = seckillService.getById(seckillId);
    if (seckill == null) {
      return "forward: /seckill/list";
    }
    model.addAttribute("seckill", seckill);
    return "detail";
  }

  @PostMapping(value = "/{seckillId}/exposer",
    produces = {"application/json; charset=UTF-8"})
  @ResponseBody
  public SeckillResult exposer(@PathVariable Long seckillId) {

    Exposer exposer;
    try {
      exposer = seckillService.exportSeckillUrl(seckillId);
    } catch (Exception e) {
      _log.error("Fail to export seckill url", e);
      return ResultUtil.fail(e.getMessage());
    }
    return ResultUtil.success(exposer);
  }

  @PostMapping(value = "/{seckillId}/{md5}/execution",
    produces = {"application/json; charset=UTF-8"})
  @ResponseBody
  public SeckillResult execute(@PathVariable Long seckillId,
                               @PathVariable String md5,
                               @CookieValue(value = "killPhone", required = false) Long phone) {

    if (phone == null) {
      return ResultUtil.fail("未注册");
    }

    SeckillExecution execution;
    try {
      execution = seckillService.executeSeckill(seckillId, phone, md5);
    } catch (RepeatKillException e1) {
      return ResultUtil.fail(SeckillState.REPEAT_KILL.getStateInfo());
    } catch (SeckillCloseException e2) {
      return ResultUtil.fail(SeckillState.END.getStateInfo());
    } catch (Exception e) {
      _log.error("Fail to export seckill url", e);
      return ResultUtil.fail(SeckillState.INNER_ERROR.getStateInfo());
    }

    return ResultUtil.success(execution);
  }

  @GetMapping(value = "/time/now",
    produces = {"application/json; charset=UTF-8"})
  @ResponseBody
  public SeckillResult time() {
    return ResultUtil.success(new Date());
  }

}
